<template>
  <div class="login">
    <div class="content">
      <div class="dec"></div>
      <el-form
        :model="user"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="userForm"
      >
        <el-form-item>
          <span>Login</span>
        </el-form-item>
        <el-form-item prop="tel" ref="tel">
          <el-input type="text" v-model="user.tel" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="password" v-if="byCode">
          <el-input type="password" v-model="user.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="msgcode" v-if="!byCode">
          <el-input type="text" v-model="user.msgcode" placeholder="请输入验证码"></el-input>
        </el-form-item>
        <el-form-item v-if="byCode">
          <el-checkbox v-model="checked" class="rem">记住密码</el-checkbox>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <el-link :underline="false" @click.prevent="byCode=!byCode">验证登录</el-link>
        </el-form-item>
        <el-form-item v-if="!byCode">
          <el-button class="getCode" size="small" @click.prevent="getCode">获取验证码</el-button>
          <el-link
            :underline="false"
            style="font-size:13px;position:relative; left:140px;top:-14px;"
            @click.prevent="byCode=true"
          >通过密码登录</el-link>
        </el-form-item>
        <el-form-item class="btn_login">
          <el-button type @click="submitForm('ruleForm')" size="small" round>登&nbsp;&nbsp;录</el-button>
          <el-button @click="resetForm('ruleForm')" size="small" round>重&nbsp;&nbsp;置</el-button>
        </el-form-item>
        <el-link :underline="false" class="reg" @click.prevent="goRegister">没有账号？现在就去注册</el-link>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "login",
  data: function() {
    return {
      user: {
        tel: "",
        password: "",
        msgcode: ""
      },
      byCode: true,
      checked: false,
      rules: {
        tel: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          { min: 11, max: 11, message: "手机号码必须是11位", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 6, message: "密码必须是6位", trigger: "blur" }
        ],
        msgcode: [
          { required: true, message: "验证码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      //通过ref定位到form表单
      this.$refs[formName].validate(valid => {
        if (valid) {
          // alert('submit!');
          if (this.byCode) {
            var _this = this;
            this.$axios
              .post("http://localhost:3000/users/login", this.user)
              .then(res => {
                if (res.data.data == -1) {
                  alert("手机号或密码输入错误!请重新输入");
                  this.user.password = "";
                } else {
                  console.log(res.data);
                  if (_this.checked == true) {
                    let userInfos = {
                      userTel: _this.user.tel,
                      password: _this.user.password,
                      checked: true
                    };
                   
                  } else {
                    console.log("清空Storage");
                    localStorage.removeItem("userInfo");
                  }
                  localStorage.setItem("userInfo", JSON.stringify(userInfos));localStorage.setItem("mytoken", res.data.token)
                  this.$router.push("/index/home");
                }
              })
              .catch(err => {
                console.log(err);
              });
          }else{
               this.$axios.get("http://localhost:3000/users/msgCheck",{params:{tel:this.user.tel}})
               .then(res=>{
                 console.log(res.data.data)
                    if(res.data.data==-1){
                      alert("该号码未注册，请先注册")
                       return false;
                    }else{
                       let codes=JSON.parse(sessionStorage.getItem('mycode'))
                       if(this.user.tel==codes.tel&&this.user.msgcode==codes.code){
                         this.$router.push("/index/home")
                       }else{
                         alert("手机号或验证码不正确")
                       }
                    } 
               })
               .catch(err=>{
                  console.log(err);
               })
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      //实现重置表单元素数据
      this.$refs[formName].resetFields();
    },
    getCode(){
         this.$axios.get('http://localhost:3000/users/getVCode',{params:{tel:this.user.tel}})
         .then(res=>{
           if(res.data.msg==0){
              sessionStorage.setItem('mycode',JSON.stringify(res.data.data))
           }else{
             if(res.data.data==205403){
               alert("网络错误,请重试!")
             }else if(res.data.data==205405){
               alert("号码异常/同一号码发送次数过于频繁") 
             }else if(res.data.data==205409){
                alert("系统繁忙，请稍后重试") 
             }else if(res.data.data==205401){
                alert("错误的手机号码") 
             }else{
               alert('未知错误')
             } 
           }
         })
         .catch(err=>{
           alert("验证码请求异常")
         })
    },
    goRegister() {
      this.$router.push("/register");
    }
  },
    create(){
    let userinfo=localStorage.getItem('userInfo')
    if(userinfo){
      userinfo=JSON.parse(localStorage.getItem('userInfo'))
      this.user.tel=userinfo.useTel
      this.user.password=userinfo.password
      this.user.checked=true
    }
  }
};
</script>
<style lang="css">
.login input.el-input__inner {
  border: none;
}
.login .el-button:focus,
.login .el-button:hover {
  color: #fff;
  border-color: #fe6f42;
}
.login span.el-checkbox__input.is-checked + .el-checkbox__label {
  color: rgb(96, 98, 102);
}
.login span.el-checkbox__input.is-checked .el-checkbox__inner,
.login .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: #fe6f42;
  border-color: #fe6f42;
}
.login span.el-checkbox__inner:hover,
.login span.el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: #fe6f42;
}
</style>
<style lang="scss" scoped>
$color: #fe6f42;
.login {
  width: 100%;
  height: 760px;
  background-color: rgb(250, 195, 190);
  font-family: "KaiTi"
}
.content {
  height: 460px;
  width: 850px;
  /* border:1px solid #ccc; */
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -400px;
  margin-top: -230px;
  /* background-color: #fff; */
  background: url(../assets/登录.jpg) no-repeat;
  background-size: 100% 100%;
  box-shadow: 5px 6px 8px rgba(0, 0, 0, 0.22), 0 0 6px rgba(0, 0, 0, 0.04);
}
.dec {
  width: 450px;
  height: 380px;

  float: left;
}
.userForm {
  width: 400px;
  height: 380px;
  float: right;
  border-radius: 5px;
  padding: 50px 50px 10px 0px;
  box-sizing: border-box;
  /* opacity: 0.8; */
  position: relative;
  left: -40px;
  top: 10px;
}
.el-input {
  border-bottom: 1px solid #ccc;
}
.el-form-item span {
  font-size: 40px;
  letter-spacing: 2px;
  font-weight: 100;
  color: rgb(96, 98, 102);
}
.btn_login {
  position: relative;
  left: 20px;
}
.el-form-item:nth-child(2),
.el-form-item:nth-child(3),
.el-form-item:nth-child(4) {
  position: relative;
  top: 20px;
}

.el-button {
  margin-right: 30px;
  background-color: $color;
  color: #fff;
}
.el-button:active,
.el-button:focus,
.el-button:hover {
  background-color: $color;
  color: #fff;
  border-color: $color;
}
.getCode {
  position: absolute;
  top: -120px;
  left: 136px;
}
.reg {
  position: relative;
  left: 120px;
  top: 20px;
}
.el-link.el-link--default:hover {
  color: $color;
}
</style>